package com.vctgo.accountSet.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vctgo.accountSet.mapper.ChinasMapper;
import com.vctgo.accountSet.domain.Chinas;
import com.vctgo.accountSet.service.IChinasService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
/**
 * 地址表Service业务层处理
 *
 * @author vctgo
 * @date 2024-12-17
 */
@Service
public class ChinasServiceImpl implements IChinasService
{
    @Autowired
    private ChinasMapper chinasMapper;

    /**
     * 查询地址表
     *
     * @param id 地址表主键
     * @return 地址表
     */
    @Override
    public Chinas selectChinasById(Long id)
    {
        return chinasMapper.selectById(id);
    }

    /**
     * 查询地址表列表-分页
     *
     * @param chinas 地址表
     * @return 地址表
     */
    @Override
    public IPage<Chinas> selectChinasPage(Chinas chinas)
    {
        Page mpPage =new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM),1L)
                ,Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE),10L));
        return chinasMapper.selectChinasList(mpPage,chinas);
    }
    /**
     * 查询地址表列表
     *
     * @param chinas 地址表
     * @return 地址表
     */
    @Override
    public List<Chinas> selectChinasList(Chinas chinas)
    {
        return chinasMapper.selectChinasList(chinas);
    }

    /**
     * 新增地址表
     *
     * @param chinas 地址表
     * @return 结果
     */

    @Override
    public int insertChinas(Chinas chinas)
    {
        return chinasMapper.insert(chinas);
    }

    /**
     * 修改地址表
     *
     * @param chinas 地址表
     * @return 结果
     */
    @Override
    public int updateChinas(Chinas chinas)
    {
        return chinasMapper.updateById(chinas);
    }

    /**
     * 批量删除地址表
     *
     * @param ids 需要删除的地址表主键
     * @return 结果
     */
    @Override
    public int deleteChinasByIds(Long[] ids)
    {
        return chinasMapper.deleteChinasByIds(ids);
    }

    /**
     * 删除地址表信息
     *
     * @param id 地址表主键
     * @return 结果
     */
    @Override
    public int deleteChinasById(Long id)
    {
        return chinasMapper.deleteById(id);
    }

    @Override
    public List<Chinas> getList() {
        // 全部省数据
        List<Chinas> chinas = chinasMapper.selectList(new QueryWrapper<Chinas>().eq("pid", 0L).eq("level", "1"));

        System.out.println("-------------"+chinas);

        List<Long> pid = new ArrayList<>();
        chinas.forEach(item -> {
            pid.add(item.getCode());
        });
        //全部市数据
        List<Chinas> json1 = chinasMapper.selectList(new QueryWrapper<Chinas>().eq("level", "2").in("pid", pid));
        Map<Long,List<Chinas>> cdv=json1.stream().collect(Collectors.groupingBy(Chinas::getPid));

        chinas.stream().forEach(a ->a.setChildren(cdv.get(a.getCode())));

        List<Long> pid1=new ArrayList<>();
        json1.forEach(item ->{
            pid1.add(item.getCode());
        });
        //全部县 区数据
        List<Chinas> json2=chinasMapper.selectList(new QueryWrapper<Chinas>().eq("level","3").in("pid",pid1));
        Map<Long,List<Chinas>> cdv2=json2.stream().collect(Collectors.groupingBy(Chinas::getPid));
        chinas.stream().filter(a -> a.getChildren() != null).forEach(a -> a.getChildren().stream()
                .forEach(i -> i.setChildren(cdv2.get(i.getCode()))));
        System.out.println("=========="+chinas);
        return chinas;

    }


    @Override
    public List<Chinas> selectnihao(QueryWrapper<Chinas> wrapper) {
        List<Chinas> chinasList = chinasMapper.selectList(wrapper);
        return chinasList;
    }


}
